<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var $ = (function() {
            var o = window.XMLHttpRequest ?
                new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
            if (!o) {
                throw new Error('您的浏览器不支持异步发起的HTTP请求')
            }

            function _doAjax(opt) {
                var opt = opt || {},
                    type = (opt.type || 'GET').toUpperCase(),
                    // 如果是字符串的false就是false，否则是true
                    async = '' + opt.async === 'false' ? false : true,
                    dataType = (opt.dataType || 'JSON').toUpperCase(),
                    url = opt.url,
                    data = opt.data || null,
                    error = opt.error || function() {},
                    success = opt.success || function() {},
                    complete = opt.complete || function() {},
                    timeout = opt.timeout || 3000,
                    t = null

                if (!url) {
                    throw new Error('没有填写url')
                }
                //请求超时,兼容性不好,利用延时函数
                // o.ontimeout=function(){
                //     o.abort()
                //     o=null;
                // }
                o.onreadystatechange = function() {
                    if (o.readyState === 4) {
                        if ((o.status >= 200 && o.status < 300) || o.status === 304) {

                            switch (dataType) {
                                case 'JSON':
                                    success(JSON.parse(responseText));
                                    break;
                                case 'TEXT':
                                    success(responseText);
                                    break;
                                case 'XML':
                                    success(responseXML);
                                    break;
                                default:
                                    success(JSON.parse(responseText));
                                    break;
                            }
                        } else {
                            error();
                        }
                        complete();
                        clearTimeout(t);
                        t = null;
                        o = null;
                    }

                }
                o.open(type, url, async);
                // 如果是post请求，设置请求头中content-type
                type === 'POST' && o.setRequestHeader('Content-type', 'application/x-www-from-urlencoded');
                // 如果是post请求，传输参数格式 xxx=xxx&xxx=xxx
                o.send(type === 'GET' ? null : formatDatas(data))
                    // 延时请求时间
                t = setTimeout(
                    function() {
                        complete();
                        o.abort();
                        clearTimeout(t)
                        t = null;
                        o = null;

                        throw new Error('This request has been timeout for' + url);

                    }, timeout
                )
            }

            function formatDatas(obj) {
                var str = '';
                for (var key in obj) {
                    str += key + '=' + obj[key] + '&';
                }
                console.log(str.replace(/&$/, ''));
                return str.replace(/&$/, '')
            }
            return {
                Ajax: function(opt) {
                    _doAjax(opt)
                },
                post: function(url, data, callback) {
                    _doAjax({
                        type: 'post',
                        url: url,
                        data: data,
                        success: callback,
                    })
                },
                get: function(url, callback) {
                    _doAjax({
                        url: url,
                        success: callback,
                    })
                },
            }
        })()
        $.Ajax({
            url: 'www.112.com',
            type: 'post',
            data: {
                name: 1,
                age: 123
            }
        })
    </script>
</body>

</html>